61
Explore Your Deductive Logic—Sudoku
61
(2,6)
(2,9)
(2,9)
(2,6)
(2,9)
COLUMN 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
ROW
FIGURE 3.9 Matching twins.
In the picture above, the “twins” are identified with the two possible values they
can have. Starting from the cell in row 8 column 2, there are two possibilities, 2 and
6. If you assume it is 6 and follow the red arrow, then the cell in row 5 column 9 is
9. If you assume it is 2, and follow the blue arrow, then the cell in row 5 column 9 is
9 as well. Thus “9” is a keeper for the cell in row 5 column 9.
This algorithm also introduces you to the concept of “recursion”. I will explain
that shortly. But here are the steps to make this algorithm work:
The first step is to take a snapshot of the current 9 by 9 grid so we can compare it
later on.
The second step is to make a list of all the cells that have two values since we will
have to iterate through this list trying one value each time.
The third step is to pick one of the two-value cells and fill it with one of the two
values, run through the rest of the logic and note all the empty cells that got filled—
let’s call these cells Set 1. Now do the same with the second value and note the empty
cells that got filled. Let’s call these cells Set 2. Then compare Set 1 with Set 2. If any
cell in Set 1 has the same value as Set 2, then this is a keeper. Make a list of these
keepers. Let’s call these cells Set 3.
The fourth and final step is to restore the snapshot we took in step 1 and then
replace the cells in Set 3 with the keepers. We are done!
Here is the code that implements this algorithm.
STEP 21
First 2 steps to take snapshot and make list of two-value cells.
:
‘ algorithm to perform matching twins
‘ * Step 1 take snapshot
For i = 1 To 9
For j = 1 To 9
snapshot(i, j) = Cells(i, j)
putnumberpresent(i, j) = ““